package zengjin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.common.PageResponse;
import zengjin.mapper.NewMapper;
import zengjin.model.dto.news.AddNewRequest;
import zengjin.model.dto.news.PageNewRequest;
import zengjin.model.en.New;
import zengjin.model.vo.news.PageNewVO;
import zengjin.service.NewService;

/**
* @author 19324
* @description 针对表【new】的数据库操作Service实现
* @createDate 2025-02-26 11:31:05
*/
@Service
public class NewServiceImpl extends ServiceImpl<NewMapper, New>
    implements NewService {

    @Resource
    private NewMapper newMapper;

    @Override
    public Boolean addNew(AddNewRequest addNewRequest) {
        //1. 获取参数
        Long newCreatorId = addNewRequest.getNewCreatorId();
        Long newReceiverId = addNewRequest.getNewReceiverId();
        String newCreatorName = addNewRequest.getNewCreatorName();
        String newCreatorAvatar = addNewRequest.getNewCreatorAvatar();
        String newReceiverName = addNewRequest.getNewReceiverName();
        String newTitle = addNewRequest.getNewTitle();
        String newText = addNewRequest.getNewText();

        //2. 调用mapper
        New news = new New();
        news.setNewCreatorId(newCreatorId);
        news.setNewReceiverId(newReceiverId);
        news.setNewCreatorName(newCreatorName);
        news.setNewCreatorAvatar(newCreatorAvatar);
        news.setNewReceiverName(newReceiverName);
        news.setNewTitle(newTitle);
        news.setNewText(newText);

        return newMapper.insert(news) > 0;
    }

    @Override
    public PageResponse<PageNewVO> pageNew(PageNewRequest pageNewRequest) {
        //1. 获取参数
        Long newReceiverId = pageNewRequest.getNewReceiverId();

        //2. 构造条件
        Page<New> page = pageNewRequest.toPageDefaultSortByUpdateTime();

        Page<New> news = lambdaQuery()
                .eq(newReceiverId != null, New::getNewReceiverId, newReceiverId)
                .page(page);
        return PageResponse.of(news, PageNewVO.class);
    }
}




